<link rel='stylesheet' type='text/css' href="<?php echo Yii::app()->theme->baseUrl; ?>/css/jquery-ui-1.8.14.custom.css" />
<script type='text/javascript' src="<?php echo Yii::app()->theme->baseUrl; ?>/fullCalendar/jquery/jquery-ui-1.8.11.custom.min.js"></script>
<script type='text/javascript' src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery.ui.datepicker.js"></script>
<script type='text/javascript' src="<?php echo Yii::app()->theme->baseUrl; ?>/fullCalendar/jquery/jquery-ui-1.8.11.custom.min.js"></script>

<script type="text/javascript"><!--
function HidePart(item) { 
        document.getElementById(item).style.display = "none";
        }
function ShowPart(item) { 
        document.getElementById(item).style.display = ""; 
        
}

$(document).ready(function() {
        $(".popupCancel").click(function() {
                $.fancybox.close();
                });
        $(".blControl").hide();
        $(".bugListRow").mouseover(function() {
			$(this).children(".blControl").show();
            });
        $(".bugListRow").mouseout(function() {
			$(this).children(".blControl").hide();
            });
        $("#bugHappenedDate").datepicker();
       
// 		$(".addBugBtn").each(function(){
// 		 		$(this).click( function(){
//     			nid = $(this).attr('id');
//     		    //alert(nid) ;
//     		    $("#addBugForm").append('<input type="hidden" name="bug_list_id" value="'+nid+'"></input>');
//     		    
//                // #: id . :class
//               
//     		
// 		});
// 		});
 		//Delete Bug List ID 
		$(".deleteBLLink").each(function(){
			$(this).click(function(){
			  var answer=confirm('Do you want to delete this ?');
			  if(answer){
				 nblid=$(this).attr('name');
				 blid=nblid.split('-')[0];
				 pid=nblid.split('-')[1];
				 
				 $.ajax({
				 type:'POST',
					url:'<?php echo $this->createUrl("/bugs/deleteBugList");?>',
					data:{
						blid:blid,
						pid:pid,
					},
					success:function(data){
						//alert("Delete complete");
						if(data ==1)
							window.location.reload();
					},
					error:function(data){
						alert("Error");
					}
				 });
				 }
				 return false;
			});
		});
 		//begin edit Bug list
 		$(".editBLLink").each(function(){
               $(this).click(function(){
                   blid=$(this).attr('name');
					//alert(blid);
                   $("#editBugForm").append('<input type="hidden" name="bug_list_id" value="'+blid+'"></input>'); 
                   
                   });
 	 		});
			$("#editListButton").click(function(){
				var blName = $("#editBugForm .editBLName").val();
				var blDescript =$("#editBugForm #editcateDes").val();
				var bugListId=$("#editBugForm input[name=bug_list_id]").val();
			 alert(blName+"-"+blDescript+"-"+bugListId );
					$.ajax({
					url:'<?php echo $this->createUrl('bugs/editBugList')?>',
					type:'POST',
					data:{
						blName: blName,
						blDescript: blDescript,
						bugListId: bugListId,
						},
						success:function(data){
							//alert (data);
							if(data==1){
								window.location.reload();
								}
							},
						error:function(data){
							//alert(url);
							alert("loi khi goi action");
							}
						
					}
				
			);
				return false;
				});
 		//end edit Bug list
	
      //function add them bug 
 	$("#addBugButton").click(function(){
 	 	 // lay toan bo attribute;
           var bugName = $("#addBugForm .BugName").val();
           var bugPriority = $("#addBugForm select").val();
           var description = $("#addBugForm textarea").val();
           var happenedDate =$("#addBugForm #bugHappenedDate").val();
           var bugListId =$("#addBugForm input[name=bug_list_id]" ).val();
          //if(bugName == NULL)$("#validator").html("Bug Name can\'t not be blank");
          
           $.ajax({
				url: '<?php echo $this->createUrl('bugs/addbug')?>',
				type:'POST',
				data:{
					bugName: bugName ,
					happenedDate: happenedDate,
					bugPriority:bugPriority,
					description:description,
					bugListId:bugListId,
					},
				success:function(data){
                       //append vao day
                    //alert(data);
                    if(data == 1){
                     //append code
                        window.location.reload();
                    }
                    else if(data==2){
                        
                       $("#validator").html("Bug Name has been existed"); 
                        }
					},
					error:function(data){
						alert ('can\'t call action');
						}
               });
           return false;
 	 	});
		});
//Time picker


--></script>
<?php 
 $jsFilterUser = "
        $(\"#filterUser\").change(function()
        {
            document.location.href = $(this).val();
        });
    ";
  Yii::app()->clientScript->registerScript('FilterUser',$jsFilterUser,CClientScript::POS_READY);
?>
<div class="clear"></div>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/bug.css" />

                                <div id="than"> 
                                <img src="<?php echo Html::imageUrl("muiten.png");?>" width="27" height="26"; style="float:left">
                        <div id="textxanhdam">Bugs </div>
                        </div>
                <div class="padding15"></div>                           
                <!-- Báº¯t Ä�áº§u Cá»™t TrÃ¡i-->                  
                <div id="leftSidebar">
                        <div id="leftWrapper">
                                                <button type="button" id="addBugListButton" href="#addBugList">Add new bug list</button>
                                                <div class="padding20"></div>
                                                
                                                Filter by user
                                                <div class="padding5"></div>
                                               <!-- List all user in select box -->
                            <select id="filterUser" "style="width:290px margin-left:10px;">
                                    <?php
                                        $pid = Yii::app()->request->getParam("pid");
                                        echo "<option value='{$this->createUrl('/bugs/index',array('pid'=>$pid))}'>Filter by user</option>";
                                        echo "<option value='{$this->createUrl('/bugs/index',array('pid'=>$pid))}'>All users</option>";
                                        foreach($users as $user){
                                            $user = User::model()->findByPk($user->project_user_user_id);
                                            $link = $this->createUrl('/bugs/index',array('pid'=>$pid,'uid'=>$user->user_id));
                                            echo "<option value='{$link}'>{$user->user_name}</option>";
                                        }
                                    ?>
                            </select>
                               
                                
                                <div class="padding15"></div>
                                Active bug lists
                                <div class="padding5"></div>
                                <div id="taskList">
                                        <ul class="tasks">
                                              
											 <?php foreach($AllBugList as $item){?>
												<li><?php echo CHtml::link($item->bug_list_name,$this->createUrl('bugs/index',array('pid'=>$pid,'blid'=>$item->bug_list_id)));?></li>
												<?php } //end show allbug list?>
											
                                        </ul>                           
                                                </div>
                                        </div>
                                <!-- Káº¿t ThÃºc Cá»™t TrÃ¡i-->
                                </div>
                
                <!-- Báº¯t Ä�áº§u Cá»™t Pháº£i-->
                <div  id="rightSidebar">
                        <div id="rightHeader">
                                                <h2>Bugs<h2>
                                                <?php Yii::app()->user->getFlash('index');?>
                                                <button type="button" id="rightHeaderBtn" href="#addBugList">Add a bug list</button>
                                        </div>
                                        <div id="rightWrapper">
                                        <?php
                                          $sttTable=0;  
                                          $time=date('Y-m-d h:m:s');
                                          foreach($Bugs as $item){
                                                   $sttTable++;
                                                   $BugList=$item['model'];
                                                   $BugInList=$item['AllBug'];
                                                 
                                        ?>
                                        <div class="bugListRow">
                                                        <h3><?php echo CHtml::encode($BugList->bug_list_name);?></h3>
                                                        <div class="blControl">
                                             
                                                        	
															<?php
																echo Html::ajaxLink("Edit","#editBL",
																	array(
																	 
														           'beforeSend'=>"function(){
														           $('.editBLName').val('$BugList->bug_list_name');
                                          							$('#editcateDes').val('$BugList->bug_list_description');	
                                          							}",
																	),
																	array(
																		'name'=>$BugList->bug_list_id,
																		'class'=>'editBLLink',
																	    'href'=>'#editBL',
																	)
																);
															?>
															
                                                       		<a href="" name=<?php echo $BugList->bug_list_id."-".$pid ?> " class="deleteBLLink">Delete</a>	
                                                        </div>
                                                </div>
                                                <div class="padding5"></div>
                                                <!-- List of bugs -->
                                                <table class="bugTable" id="<?php echo "bug-".$BugList->bug_list_id?>">
                                                        <thead></thead>
                                                        <tr>
                                                                <th class="bugNo">No.</th><th>Bug name</th><th class="bugPriority">Priority</th><th>Created by</th><th>Time</th>
                                                        </tr>

                                                        <?php 
                                                          $i=0;
                                                          $countCompleteBug = 0;
                                                          foreach($BugInList as $Bug){
                                                           $i++;?>
                                                            <?php 
                                                            if($Bug->bug_completed_date == NULL || $Bug->bug_completed_date > $time){
                                                            ?>
                                                        
                                                          <tr>
                                                                <td><?php echo CHtml::encode($i);?></td>
                                                                <td><?php echo CHtml::link($Bug->bug_name,$this->createUrl('/bugs/bugDetail',array('bid'=> $Bug->bug_id,'pid'=>$pid)));?></td>
                                                                <?php 
                                                                   switch($Bug->bug_priority){
                                                                        case 1: $class= "bugHigh>"." High</td";
                                                                          
                                                                                        break;
                                                                        case 2: $class= "bugMedium>"."Medium </td";
                                                                                        break;
                                                                        case 3:
                                                                                $class= "bugLow >"."Low </td";
                                                                                break;
                                                                        default:
                                                                                $class= "></td";
                                                                }?>
                                                                <td class=<?php echo $class; ?>>
                                                                <td><a href=""><?php 
                                                                $bugCreator = $Bug->bugPostedByUser;
                                                                echo CHtml::encode($bugCreator->user_name);?></a></td>
                                                                <td><?php echo Html::formatDate($Bug->bug_happened_date);?> </td>

                                                        </tr>
                                                        
                                              <?php } ?>
                                                                 <?php
                                                          /**
                                                           * show completed bugs
                                                           */
                                                                  
                                                          if($Bug->bug_completed_date !=NULL &&$Bug->bug_completed_date < $time ){
                                                           $countCompleteBug=1;
                                                          	?>
                                                        <tr id=<?php echo "completedBug".$sttTable; ?> style="display: none;" >
                                                                <td>4</td>
                                                                <td class="completedBug"><?php echo CHtml::link($Bug->bug_name,$this->createUrl('/bugs/bugDetail',array('bid'=> $Bug->bug_id,'pid'=>$pid)));?></td>
                                                                <?php 
                                                                   switch($Bug->bug_priority){
                                                                        case 1: $class= "bugHigh>"." High</td";
                                                                          
                                                                                        break;
                                                                        case 2: $class= "bugMedium>"."Medium </td";
                                                                                        break;
                                                                        case 3:
                                                                                $class= "bugLow >"."Low </td";
                                                                                break;
                                                                        default:
                                                                                $class= "></td";
                                                                }?>
                                                                <td class=<?php echo $class; ?>>
                                                                <td><a href=""><?php 
                                                                $bugCreator = $Bug->bugPostedByUser;
                                                                echo CHtml::encode($bugCreator->user_name);?></a></td>
                                                                <td><?php echo Html::encode($Bug->bug_happened_date);?></td>
                                                        </tr>
                                                          <?php } // end show completed bugs?>
                                                        <?php }//end show bugs on each BugList?>
                                                </table>
                                                <div class="padding10"></div>
                                                <div class="bugControlBtn">
                                                        <?php 
                                                        	echo CHtml::button('Add Bug',array(
                                                        		'class'=>'addBugBtn',
                                                        		'href'=>'#addBug',
                                                        	 	'onClick'=>"js: $('#blid').val('$BugList->bug_list_id')"
                                                        	));
                                                        ?>
                                                        <?php if($countCompleteBug !=0){?>
                                                        <button id=<?php echo "showBugs".$sttTable?> type="button" onclick="ShowPart('<?php echo "completedBug".$sttTable;?>');HidePart('<?php echo "showBugs".$sttTable?>');ShowPart('<?php echo "hideBugs".$sttTable?>');" >Show completed bugs</button>
                                                        <button id=<?php echo "hideBugs".$sttTable?> type="button" onclick="HidePart('<?php echo "completedBug".$sttTable;?>');ShowPart('<?php echo "showBugs".$sttTable?>');HidePart('<?php echo "hideBugs".$sttTable?>');" style="display:none;">Hide completed bugs</button>                                 
                                                <?php }?>
                                                </div>
                                                <div class="padding10"></div>
                                                        <?php
                                                      }// close $BUGLIST?>
                                                

                </div>
                   
             </div>    
    <!--Káº¿t thÃºc Pháº§n ThÃ¢n Ná»™i Dung--> 
    <!--  POP-UP -->
    <div id="addBLPopup" style="display: none;">
        <div id="addBugList" class="popupWrapper" action="" method="post">
                <div class="popupHeader">
                        <h2>New bug list</h2>
                </div>
                <div class="popupContent">
                        <h3>Bug list name</h3>
                        <div class="padding5"></div>
                        <?php 
                        $BugListForm= $this->beginWidget('CActiveForm',array(
                        'id'=>'bugList-form','enableAjaxValidation'=>false,));
                        echo $BugListForm->textField($NewBugList,'bug_list_name',array('class'=>'addBLName'));?>
                                <div class="padding10"></div>
                                <p>Description</p>
                        <?php 
                        echo $BugListForm->textArea($NewBugList,'bug_list_description',array(
                        'rows'=>4,'cols'=>70,));        
                        echo CHtml::submitButton('Create this bug list',array('class'=>'postButton'));
                        ?>
                                 or
                                        <a href="#" class="popupCancel">Cancel</a>
                                </div>
                        <?php  $this->endWidget();
                        
                        ?>
                        
                </div>
        </div>
    </div>
    <div id="addBugPopup" style="display: none;">
        <div id="addBug" class="popupWrapper" action="" method="post">
                <div class="popupHeader">
                        <h2>Add a bug</h2>
                </div>
                <div class="popupContent">
                        <h3>Bug name</h3>
                        <div class="padding5"></div>
                       <?php $addBugForm = $this->beginWidget(
                        'CActiveForm',
                       array(
                       	'action'=>$this->createUrl('/bugs/add',array('pid'=>$pid)),
                        
                       
                       )
                       );?>
                       <?php echo $addBugForm->textField($AddBug,'bug_name',array('class'=>"BugName"));
                       		 echo $addBugForm->error($AddBug,'bug_name',array('class'=>'formError'));
                       ?>
                      			<div class="padding10"></div>
                                Priority 
                                <?php 
                                //$Priority = ();
	                                echo $addBugForm->dropDownList($AddBug,'bug_priority', 
	                                $AddBug->listPriority());
                                ?>
                                <div class="padding10"></div>
                                Bug Happened Date:
                                <?php 
                                	echo $addBugForm->textField($AddBug,'bug_happened_date',array('id'=>"bugHappenedDate"));
                                	echo $addBugForm->error($AddBug,'bug_happened_date');
                                ?>
                                <div class="padding10"></div>
                                <p>Description</p>
                                <?php 
                               		echo $addBugForm->textArea($AddBug,"bug_description",array('rows'=>4 , 'cols'=>70));
                               		echo $addBugForm->error($AddBug,'bug_description');
                                ?>
                                <?php echo CHtml::activeHiddenField($AddBug, 'bug_list_id',array('id'=>'blid'));
                               
           						?>
                                <div class="postButton">
                                        <?php echo CHtml::submitButton('Add Bug')?> or
                                        <?php echo CHtml::link('Cancel',"#",array('class'=>'popupCancel'));?>
                                </div>
                        <?php $this-> endWidget();?>
                        <div class="padding10"></div>
                       

                </div>
        </div>
    </div>
    
    <!-- Begin edit bug list popup -->
	<div id="editBLPopup" style="display:none;">
		<div id="editBL" class="popupWrapper" method="post" action="#">
			<div class="popupHeader">
				<h2>Edit bug list</h2>
			</div>
			<div class="popupContent">
				<form method="post" action="" id="editBugForm">
					<h3>Bug list name</h3>
					<div class="padding5"></div>
					<input type="text" class="editBLName" size="80px" ></input>
					
					
					<div class="padding10"></div>
					<h3>Bug list description</h3>
					<textarea id="editcateDes"rows="4" cols="70"></textarea>
					<div class="padding10"></div>
					<div class="postButton">
						<input id= "editListButton"type="button" value="Update this bug list" /> or
						<a href="#" class="popupCancel">Cancel</a>
					</div>
				</form>
			</div>
		</div>
	</div>
    
<?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'a.editBLLink', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false, 'hideOnOverlayClick' =>false ), ) ); ?>    
<?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'button#rightHeaderBtn', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false, 'hideOnOverlayClick' =>false ), ) ); ?>
<?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'button#addBugListButton', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false,  'hideOnOverlayClick' =>false), ) ); ?>
<?php  $this->widget('application.extensions.fancybox.EFancyBox', array( 'target'=>'input.addBugBtn', 'config'=>array( 'scrolling' => 'no', 'titleShow' => false, 'hideOnOverlayClick' =>false ), ) ); ?>
